
layui.config({
    base: basePath,
    version: skyeyeVersion
}).extend({
    window: 'js/winui.window'
}).define(['window', 'table', 'jquery', 'winui', 'form'], function (exports) {
    winui.renderColor();
    var $ = layui.$,
        form = layui.form,
        table = layui.table;

    var businessClassName = GetUrlParam("objectId");
    var appId = GetUrlParam("appId");

    var printTemplate = {
        // 初始化枚举数据
        initEnumData: {},

        // 当前模板信息
        currentTemplate: {},

        // 初始化表格
        initTable: function () {
            table.render({
                id: 'messageTable',
                elem: '#messageTable',
                method: 'post',
                url: reqBasePath + 'queryPrintTemplateList',
                where: printTemplate.getTableParams(),
                even: false,
                page: true,
                limits: getLimits(),
                limit: getLimit(),
                cols: [[
                    { title: systemLanguage["com.skyeye.serialNumber"][languageType], type: 'numbers', fixed: 'left' },
                    { field: 'name', title: '模板名称', width: 150, fixed: 'left' },
                    {
                        field: 'paperSize', title: '纸张大小', width: 100, align: 'center', templet: function (d) {
                            return skyeyeClassEnumUtil.getEnumDataNameByCodeAndKey("paperSize", 'id', d.paperSize, 'name');
                        }
                    },
                    {
                        field: 'orientation', title: '方向', width: 100, align: 'center', templet: function (d) {
                            return skyeyeClassEnumUtil.getEnumDataNameByCodeAndKey("orientation", 'id', d.orientation, 'name');
                        }
                    },
                    {
                        field: 'isDefault', title: '是否默认', width: 100, align: 'center', templet: function (d) {
                            return skyeyeClassEnumUtil.getEnumDataNameByCodeAndKey("commonIsDefault", 'id', d.isDefault, 'name');
                        }
                    },
                    {
                        field: 'enabled', title: '状态', width: 100, align: 'center', templet: function (d) {
                            return skyeyeClassEnumUtil.getEnumDataNameByCodeAndKey("commonEnable", 'id', d.enabled, 'name');
                        }
                    },
                    { field: 'remark', title: '描述', width: 200 },
                    { field: 'createName', title: systemLanguage["com.skyeye.createName"][languageType], width: 120 },
                    { field: 'createTime', title: systemLanguage["com.skyeye.createTime"][languageType], align: 'center', width: 150 },
                    { field: 'lastUpdateName', title: systemLanguage["com.skyeye.lastUpdateName"][languageType], align: 'left', width: 120 },
                    { field: 'lastUpdateTime', title: systemLanguage["com.skyeye.lastUpdateTime"][languageType], align: 'center', width: 150 },
                    { title: systemLanguage["com.skyeye.operation"][languageType], width: 350, align: 'center', fixed: 'right', toolbar: '#tableBar' }
                ]],
                done: function (res, curr, count) {
                    // 表格渲染完成回调
                    matchingLanguage();
                }
            });

            // 监听表格操作
            table.on('tool(messageTable)', function (obj) {
                var data = obj.data;
                var layEvent = obj.event;

                if (layEvent === 'preview') {
                    // 预览模板
                    printTemplate.previewTemplate(data);
                } else if (layEvent === 'design') {
                    // 设计模板
                    printTemplate.designTemplate(data);
                } else if (layEvent === 'edit') {
                    // 编辑模板
                    printTemplate.editTemplate(data);
                } else if (layEvent === 'copy') {
                    // 复制模板
                    printTemplate.copyTemplate(data);
                } else if (layEvent === 'delet') {
                    // 删除模板
                    printTemplate.deleteTemplate(data);
                }
            });
        },

        // 加载事件数据
        loadEvent: function (callback) {
            form.render();
            $("body").on("click", "#reloadTable", function () {
                printTemplate.loadTable();
            });

            // 添加
            $("body").on("click", "#addBean", function () {
                parent._openNewWindows({
                    url: "../../tpl/printTemplate/write.html?businessClassName=" + businessClassName + "&appId=" + appId,
                    title: systemLanguage["com.skyeye.addPageTitle"][languageType],
                    pageId: "actFlowAdd",
                    area: ['90vw', '90vh'],
                    callBack: function (refreshCode) {
                        winui.window.msg(systemLanguage["com.skyeye.successfulOperation"][languageType], { icon: 1, time: 2000 });
                        printTemplate.loadTable();
                    }
                });
            });
            if (typeof callback === "function") {
                callback();
            }
        },

        loadTable: function () {
            table.reloadData("messageTable", { where: printTemplate.getTableParams() });
        },

        getTableParams: function () {
            return $.extend(true, { objectKey: businessClassName }, initTableSearchUtil.getSearchValue("messageTable"));
        },

        // 编辑模板
        editTemplate: function (data) {
            parent._openNewWindows({
                url: "../../tpl/printTemplate/write.html?id=" + data.id + "&businessClassName=" + businessClassName + "&appId=" + appId,
                title: systemLanguage["com.skyeye.editPageTitle"][languageType],
                pageId: "actFlowEdit",
                area: ['90vw', '90vh'],
                callBack: function (refreshCode) {
                    winui.window.msg(systemLanguage["com.skyeye.successfulOperation"][languageType], { icon: 1, time: 2000 });
                    printTemplate.loadTable();
                }
            });
        },

        // 复制模板
        copyTemplate: function (data) {
            parent.layer.confirm('确定要复制此模板吗？', { icon: 3, title: '提示' }, function (index) {
                parent.layer.close(index);
                // 发送复制请求
                AjaxPostUtil.request({
                    url: reqBasePath + 'copyPrintTemplateById',
                    params: { id: data.id },
                    type: 'json',
                    method: "POST",
                    callback: function (json) {
                        winui.window.msg('复制成功', { icon: 1, time: 2000 });
                        // 刷新表格
                        printTemplate.loadTable();
                    }
                });
            });
        },

        // 删除模板
        deleteTemplate: function (data) {
            parent.layer.confirm(systemLanguage["com.skyeye.deleteOperationMsg"][languageType], { icon: 3, title: systemLanguage["com.skyeye.deleteOperation"][languageType] }, function (index) {
                parent.layer.close(index);
                AjaxPostUtil.request({
                    url: reqBasePath + "deletePrintTemplateById", params: { id: data.id }, type: 'json', method: "DELETE", callback: function (json) {
                        winui.window.msg(systemLanguage["com.skyeye.deleteOperationSuccessMsg"][languageType], { icon: 1, time: 2000 });
                        printTemplate.loadTable();
                    }
                });
            });
        },

        // 预览模板
        previewTemplate: function (data) {
            parent._openNewWindows({
                url: '../../tpl/printTemplate/preview.html?id=' + data.id,
                title: '模板预览',
                pageId: "previewTemplate",
                area: ['90vw', '90vh'],
                callBack: function (refreshCode) {
                }
            });
        },

        // 设计模板
        designTemplate: function (data) {
            parent.parent._openNewWindows({
                url: '../../tpl/printTemplate/design.html?id=' + data.id + '&businessClassName=' + businessClassName + '&appId=' + appId,
                title: data.name + ' - 打印模板设计',
                pageId: "designTemplate",
                area: ['100vw', '100vh'],
                callBack: function (refreshCode) {
                }
            });
            printTemplate.currentTemplate = data;
        }
    };

    // 加载事件
    printTemplate.loadEvent(function () {
        // 初始化表格
        printTemplate.initTable();
    });

    exports('printTemplate', printTemplate);
}); 